<?php
class Back_IndexController extends Osml_Controller_Admin_Action
{

    public function init()
    {
        parent::init();
        $ajaxContext = $this->_helper->getHelper('AjaxContext');
        $ajaxContext->setAutoJsonSerialization(true);
        $ajaxContext->addActionContext('classementjson', array(
            'json'
        ))->initContext();
    }

    public function indexAction()
    {
        // action body
    }

    public function connexionAction()
    {
        $this->getHelper("Layout")->setLayout('login');
        
        $form = new Back_Form_Login();
        
        $request = $this->getRequest();
        $message = __("L'authentification a échoué. Les identifiants fournis n'ont pas été trouvés dans la base de données.");
        $messageTitre = __("Authentification échouée");
        $flagConnexion = false;
        
        if ($request->isPost()) {
            if ($form->isValid($request->getPost())) {
                
                $mdp = $request->getParam("motdepasse");
                $login = $request->getParam("login");
                $salt = Back_Model_Utilisateurs::recuperationSalt($login);
                
                $db = Jnd_App::getResource("db");
                $adapter = new Zend_Auth_Adapter_DbTable($db, "utilisateurs", "pseudo", "mot_de_passe");
                $adapter->setIdentity($login)->setCredential(Jnd_Outils::generateHash($mdp, $salt));
                
                $result = $adapter->authenticate();
                if ($result->isValid()) {
                    $storage = Zend_Auth::getInstance()->getStorage();
                    $storage->write($adapter->getResultRowObject(array(
                        "id_utilisateurs", 
                        "nom", 
                        "prenom", 
                        "pseudo"
                    ), array(
                        "motdepasse"
                    )));
                    
                    $env = Jnd_App::getEnvironnement();
                    $env->idutilisateurs = $storage->read()->id_utilisateurs;
                    $env->nom = $storage->read()->nom;
                    $env->prenom = $storage->read()->prenom;
                    $env->pseudo = $storage->read()->pseudo;
                    
                    $flagConnexion = true;
                    
                    $this->redirect("index");
                }
            }
        }
        $this->view->message = $message;
        $this->view->messageTitre = $messageTitre;
        $this->view->messageFlag = $flagConnexion;
        
        $this->view->form = $form;
    }

}

